﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ShopOnline
{
    [Serializable]
    public class ThemeRec
    {
        #region Private Members
        private Int64 m_id = 0;
        private String m_name = String.Empty;
        private Decimal m_price = 0;
        private String m_description = String.Empty;
        private DateTime m_createDate = DateTime.MinValue;
        private String m_link = String.Empty;
        private int m_status = -1;
        #endregion

        #region Public Properties
        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Decimal price
        {
            get { return m_price; }
            set { m_price = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime createDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String link
        {
            get { return m_link; }
            set { m_link = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public int status
        {
            get { return m_status; }
            set { m_status = value; }
        }

        #endregion

        #region  Properties Const

        private const string ConnectName = "ShopOnline";
        private const string Folderlogs = "Theme";

        #endregion

        #region Store Procedure
        private String usp_GetSingle_Theme = "usp_GetSingle_Theme";
        private String usp_GetList_Theme = "usp_GetList_Theme";
        private String usp_Update_Theme = "usp_Update_Theme";
        private String usp_Add_Theme = "usp_Add_Theme";
        #endregion

        #region Init
        public ThemeRec() { }
        public ThemeRec(Int64 id, String name, Decimal price, String description, DateTime createDate, String link, int status)
        {
            m_id = id;
            m_name = name;
            m_price = price;
            m_description = description;
            m_createDate = createDate;
            m_link = link;
            m_status = status;
        }
        #endregion

        #region Call Store Procedure
        public List<ThemeRec> GetAll(int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetAll - PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<ThemeRec> records = new List<ThemeRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetList_Theme, PageSize, PageNumber);

                #region Default variables
                Int64 id = 0;
                String name = String.Empty;
                Decimal price = 0;
                String description = String.Empty;
                DateTime createdate = DateTime.MinValue;
                String link = String.Empty;
                int status = -1;
                #endregion

                if (dsResult.Tables.Count == 2)
                {
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("price") == false)
                            price = (Decimal)row["price"];
                        if (row.IsNull("description") == false)
                            description = row["description"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("link") == false)
                            link = row["link"].ToString();
                        if (row.IsNull("status") == false)
                            status = (int)row["status"];
                        records.Add(new ThemeRec(id,name,price,description,createdate,link,status));
                    }

                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }



                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                }

                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<ThemeRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public ThemeRec GetSingle(Int64 ThemeID)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetSingle - ThemeID:" + ThemeID;
            try
            {
                ThemeRec objTheme = new ThemeRec();

                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetSingle_Theme,ThemeID );

                if (dsResult.Tables[0].Rows.Count > 0)
                {
                    objTheme.id = Convert.ToInt64(dsResult.Tables[0].Rows[0]["id"].ToString());
                    if (!dsResult.Tables[0].Rows[0].IsNull("name"))
                    {
                        objTheme.name = dsResult.Tables[0].Rows[0]["name"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("price"))
                    {
                        objTheme.price = (Decimal)dsResult.Tables[0].Rows[0]["price"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("description"))
                    {
                        objTheme.description = dsResult.Tables[0].Rows[0]["description"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("createDate"))
                    {
                        objTheme.createDate = (DateTime)dsResult.Tables[0].Rows[0]["createDate"];
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("link"))
                    {
                        objTheme.link = dsResult.Tables[0].Rows[0]["link"].ToString();
                    }
                    if (!dsResult.Tables[0].Rows[0].IsNull("status"))
                    {
                        objTheme.status = (int)dsResult.Tables[0].Rows[0]["status"];
                    }
                    
                }
                else
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    objTheme = null;
                }

                return objTheme;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return null;
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public ObjectResult UpdateTheme(Int64 ThemeID, string ThemeName, Decimal ThemePrice, string ThemeDescription, string ThemeLink, int ThemeStatus)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "UpdateTheme - ThemeID:" + ThemeID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Update_Theme, ThemeID,ThemeName,ThemePrice,ThemeDescription,ThemeLink,ThemeStatus);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public ObjectResult AddTheme(Int64 ThemeID, string ThemeName, Decimal ThemePrice, string ThemeDescription, string ThemeLink)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "AddTheme - ThemeID:" + ThemeID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Add_Theme, ThemeID, ThemeName, ThemePrice, ThemeDescription, ThemeLink);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }
        #endregion
    }
}
